package grupobbva.pe.operhc.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class ValidationUtil {

	public static String completeLengthField(String str, int size) {
		
		int diference = size;
		if(StringUtils.isNotBlank(str)){
			str = str.trim();
			int lengthValue = str.length();
			int lengthField = size;
			diference = lengthField - lengthValue;			
		}else{
			str = "";
		}
		
		if(diference > 0){
			for (int i = 0; i < diference; i++) {
				str = str + " ";
			}
		}
		
		return str;
	}
	
	public static boolean isNumber(String parameter){
		try{
			Double.parseDouble(parameter);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public static String getDateFormat(long datetime){
		
        Date d = new Date(datetime);
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
        return sdf.format(d);
	}
	
	public static String getCurrentDate(){
		
		Date fecha = new Date(); 
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy"); 
        return sdf.format(fecha); 
	}
	
	public static String getSizeKB(long sizeBytes){
		
		DecimalFormatSymbols decimalSymbols = new DecimalFormatSymbols();
		decimalSymbols.setDecimalSeparator('.');
		
		DecimalFormatSymbols integerSymbols = new DecimalFormatSymbols();
		integerSymbols.setGroupingSeparator(',');
	
		String formatDecimal = "####0.###";
		String formatInteger = "#,##0.###";
		
		DecimalFormat formatterDecimal = new DecimalFormat(formatDecimal, decimalSymbols);
		DecimalFormat formatterInteger = new DecimalFormat(formatInteger, integerSymbols);
		formatterInteger.setGroupingSize(3);
		
		BigDecimal decimalByte = new BigDecimal(sizeBytes);
		BigDecimal decimalKB = decimalByte.divide(new BigDecimal("1024"));		
		
		String strKB = formatterDecimal.format(decimalKB);
		String[] arrayStr = strKB.split("[.]");
		long realSizeKB = Long.parseLong(arrayStr[0]);
		
		if(arrayStr.length > 1){
			long decimal = Long.parseLong(arrayStr[1]);			
			if(decimal > 1){
				realSizeKB += 1;
			}
		}
		
		return formatterInteger.format(realSizeKB);
	}
	
	public static long getNumSizeKB(long sizeBytes){
		
		DecimalFormatSymbols decimalSymbols = new DecimalFormatSymbols();
		decimalSymbols.setDecimalSeparator('.');
		
		DecimalFormatSymbols integerSymbols = new DecimalFormatSymbols();
		integerSymbols.setGroupingSeparator(',');
	
		String formatDecimal = "####0.###";
		String formatInteger = "#,##0.###";
		
		DecimalFormat formatterDecimal = new DecimalFormat(formatDecimal, decimalSymbols);
		DecimalFormat formatterInteger = new DecimalFormat(formatInteger, integerSymbols);
		formatterInteger.setGroupingSize(3);
		
		BigDecimal decimalByte = new BigDecimal(sizeBytes);
		BigDecimal decimalKB = decimalByte.divide(new BigDecimal("1024"));		
		
		String strKB = formatterDecimal.format(decimalKB);
		String[] arrayStr = strKB.split("[.]");
		long realSizeKB = Long.parseLong(arrayStr[0]);
		
		if(arrayStr.length > 1){
			long decimal = Long.parseLong(arrayStr[1]);			
			if(decimal > 1){
				realSizeKB += 1;
			}
		}
		
		return realSizeKB;
	}
	
	public static String getCurrentDateAll(){
		
		Date fecha = new Date(); 
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); 
        return sdf.format(fecha); 
	}
}
